package hsp.admin.app.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hsp.admin.api.ApiResult;
import hsp.admin.app.base.po.UserRoleRelationPO;
import hsp.admin.app.base.service.IUserRoleRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "RoleAppRelationController", description = "用户角色关系表")
@RestController
@RequestMapping("base/user/role/relation")
public class UserRoleRelationController {

    private final IUserRoleRelationService userRoleRelationService;

    @Autowired
    public UserRoleRelationController(
        IUserRoleRelationService userRoleRelationService
    ){
        this.userRoleRelationService = userRoleRelationService;
    }

    @Operation(summary = "根据用户主键获取用户角色关系")
    @GetMapping("list")
    public ApiResult<List<UserRoleRelationPO>> getUserRoleRelationList(UserRoleRelationPO po){
        LambdaQueryWrapper<UserRoleRelationPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRoleRelationPO::getUserId, po.getUserId());
        List<UserRoleRelationPO> list = userRoleRelationService.list(wrapper);
        return ApiResult.success(list);
    }

    @Operation(summary = "编辑用户角色关系")
    @PostMapping("list")
    public ApiResult<?> postRoleAppRelation(@RequestParam("userId") long userId, @RequestParam(value = "roleIds", required = false) long[] roleIds){
        userRoleRelationService.saveBatch(userId, roleIds);
        return ApiResult.success();
    }


}
